package com.hyphenate.easeui.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.controller.EaseUI;
import com.hyphenate.easeui.controller.EaseUI.EaseUserProfileProvider;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.ui.EaseChatFragment;

public class EaseUserUtils {
    
    static EaseUserProfileProvider userProvider;
    
    static {
        userProvider = EaseUI.getInstance().getUserProfileProvider();
    }
    
    /**
     * get EaseUser according username
     * @param username
     * @return
     */
    public static EaseUser getUserInfo(String username){
        if(userProvider != null)
            return userProvider.getUser(username);
        
        return null;
    }
    
    /**
     * set user avatar
     * @param username
     */
    public static void setUserAvatar(Context context, String username, ImageView imageView){

        //TODO 设置用户头像
        if(username.equals(EaseChatFragment.ANDROID)){
            Glide.with(context).load(R.drawable.head_xiaoi).into(imageView);
        }else if(username.equals(EaseChatFragment.ONLINE)){
            Glide.with(context).load(R.drawable.head_online).into(imageView);
        }else{
            EaseUser user = getUserInfo(username);
            if(!TextUtils.isEmpty(userIconPath)){
                try {
                    //取用户头像

                    Bitmap bitmap = BitmapUtils.getBitmap(userIconPath, 200, 200);
                    if (bitmap != null) {
                        imageView.setImageBitmap(bitmap);
                    }
//                    int avatarResId = Integer.parseInt(user.getAvatar());
//                    Glide.with(context).load(avatarResId).into(imageView);
                } catch (Exception e) {
                    //use default avatar
                    Glide.with(context).load(user.getAvatar()).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.head_default_user).into(imageView);
                }
            }else{
                Glide.with(context).load(R.drawable.head_default_user).into(imageView);
            }
        }


    }
    
    /**
     * set user's nickname
     */
    public static void setUserNick(String username,TextView textView){
        if(textView != null){
        	EaseUser user = getUserInfo(username);
        	if(user != null && user.getNick() != null){
        		textView.setText(user.getNick());
        	}else{
        		textView.setText(username);
        	}
        }
    }


    public static String userIconPath;

}
